from fastapi import APIRouter, HTTPException, UploadFile, File, Form, Request
from pojo.results import Result
from utils.log import logger
from utils.create_video_task import video_generate

ppt = APIRouter(prefix="/ppt")

@ppt.post("/generatVideo")
async def generate_video(file: UploadFile = File(...),
                         user_id: int = Form(...)):

    """
    利用PPT文件生成视频
    :return: 存储到minio的地址返回给用户并直接播放
    """

    try:
        if not file:
            logger.info("文件未上传成功，请重试")
            return vars(Result(400, "文件未上传成功，请重试", {"error": "No file upload"}))

        if file.filename == "":
            logger.info("文件未选中，请重试")
            return vars(Result(400, "文件未选中，请重试", {"error": "No selected file"}))

        pptx_name = file.filename
        logger.info(f"上传文件: {pptx_name}")


        # 调用视频生成函数
        data = video_generate(file, pptx_name, user_id)
        return vars(Result(200, "上传成功", data))
    except Exception as e:
        logger.error(f"文件解析失败: {e}")
        return vars(Result(500, "文件解析失败", {"error": str(e)}))




@ppt.get("/")
async def root():
    return {"message": "Hello World"}
